#[cfg(test)]
pub mod test_slice {
    use super::dslice;

    #[test]
    fn tslice() {
        dslice();
    }
}
pub fn dslice() {

    {
        /* 字符串切片 [开始下标(不填=0)..结束下标+1(不填=s.len())]

        注意:
            - 切片的索引范围必须在有效 UTF-8 字符内
            - 如果从多字节的字符中创建字符串切片， 程序会报错并退出
         */
        println!(" --------- [x..x] ------------- ");
        let s = String::from("Hello World!");
        let h1 = &s[0..5]; // "Hello" 
        let h2 = &s[..5]; // "Hello"
        let w1 = &s[6..11]; // "World"
        let w2 = &s[6..]; // "World!"
        let w3 = &s[6..s.len()]; // "World!"
        let s1 = &s[..];

        println!("h1 = '{}'", h1);
        println!("h2 = '{}'", h2);
        println!("w1 = '{}'", w1);
        println!("w2 = '{}'", w2);
        println!("w3 = '{}'", w3);
        println!("s1 = '{}'", s1);
    }

    {
        /* 字符串切片
            - 字符串字面值是切片(&str)， 直接存储在二进制程序中
            - &str 不可变，  字符串字面值也不可变

            - 一般会将切片当作函数参数, 这样可以同时接受 String 和 &str, 这样会使得函数更加通用且不损失任何功能
         */
        println!(" --------- &str / [..] ------------- ");
        let mut s = String::from("Hello World!");
        // &str: 不可变 切片
        let c = find_to_char(&s, b' ');
        // s.clear(); // 被借走了
        println!("s = {}", s);
        println!("c = {}", c);
        s.push_str("a");
        println!("s = {}", s);

        let s1 = String::from("Hello World 1!");
        let s2 = "Hello World 2!";
        let c1 = find_to_char2(&s1[..], b' ');
        let c2 = find_to_char2(&s2, b' ');
        println!("s1 = {}, c1 = {}", s1, c1);
        println!("s2 = {}, c2 = {}", s2, c2);
        // s1.clear();

    }

    {
        /* 其他类型切片
         */
        println!(" --------- other [x..x] ------------- ");
        let arr = [1, 2, 3, 4, 5];
        let slice = &arr[1..3];
        println!("slice = {}", slice.len());
    }
}


fn find_to_char(s: &String, c: u8) -> &str {
    let bytes = s.as_bytes();
    for (i, &item) in bytes.iter().enumerate() {
        if item == c {
            return &s[..i];
        }
    }
    &s[..]
}

fn find_to_char2(s: &str, c: u8) -> &str {
    let bytes = s.as_bytes();
    for (i, &item) in bytes.iter().enumerate() {
        if item == c {
            return &s[..i];
        }
    }
    &s[..]
}